﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Net;
using System.Xml;

namespace GameDataCrawler
{
    class Program
    {
        static void Main(string[] args)
        {
            //SnesGameListWikipedia();
            GameInfo("http://www.gamefaqs.com/snes/519824-super-mario-world");
        }

        private static void SnesGameListWikipedia()
        {
            //http://www.gamefaqs.com/snes/571045-zico-soccer
            //http://www.gamefaqs.com/snes/571045-zico-soccer/images
            //http://www.gamefaqs.com/snes/571045-zico-soccer/data
            //http://www.gamefaqs.com/snes/571045-zico-soccer/videos
            
            WebClient wc = new WebClient();
            string sContent = wc.DownloadString("http://www.gamefaqs.com/snes/list-999?region=0&dist=1&jpage=1&jump=Jump");
            MatchCollection mCollection = Regex.Matches(sContent, "<tr(.|\n)*?>(.|\n)*?</tr>");
            System.Text.StringBuilder sb = new StringBuilder();
            foreach (Match m in mCollection)
            {
                MatchCollection mc = Regex.Matches(m.Value, "<td>(.|\n)*?</td>");
                sb.AppendLine(Regex.Replace(mc[0].Value, "<(.|\n)*?>", "") + ";" + Regex.Match(mc[0].Value, "/(.|\n)*?\"").Value.Replace("\"", ""));
            }

            System.IO.File.WriteAllText("C:\\Luigi\\sns_lst_gamefaqs.txt", sb.ToString());
            Console.WriteLine("End.");
            Console.ReadLine();
        }

        private static void GameInfo(string sUri)
        {
            WebClient wc = new WebClient();
            string s = wc.DownloadString(sUri);
            string sContent = Regex.Match(s, "<h1>(.|\n)*?<div class=\"ad_wrap\">").Value;

            string Nome = ReplaceHTML(Regex.Match(sContent, "<h1>(.|\n)*?</h1>").Value);
            string Description = ReplaceHTML(Regex.Match(sContent, "<h2(.|\n)*?>Description</h2>(.|\n)*?</div><div(.|\n)*?</div>").Value).Replace("Description","");
            string Company = ReplaceHTML(Regex.Match(sContent, "<a href=\"/features/company/(.|\n)*?</a>").Value);
            DateTime Release = DateTime.Parse(Regex.Match(sContent, "[aA-zZ]{1,3}[ ]{1,3}[0-9]{1,2},[ ][0-9]{1,4}").Value);
            string ImageBoxShot = Regex.Match(Regex.Match(sContent, "<img class=\"boxshot\"(.|\n)*? />").Value, "http://(.|\n)*?.jpg").Value;

            //Data Tab
            string s1 = wc.DownloadString(sUri + "/data");
            sContent = Regex.Match(s1, "<b>Release Data(.|\n)*?<div class=\"pod contrib\">").Value;
            MatchCollection mCollectionData = Regex.Matches(sContent, "<tr><th scope=\"row\">(.|\n)*?</tr>");
            foreach (Match mData in mCollectionData)
            {
                string d1Name = ReplaceHTML(Regex.Match(mData.Value, "<th(.|\n)*?>(.|\n)*?</th>").Value);
                string d2Company = ReplaceHTML(Regex.Match(mData.Value, "<a(.|\n)*?>(.|\n)*?</a>").Value);
                MatchCollection mDataCollectionInfo = Regex.Matches(mData.Value, "<td(.|\n)*?>(.|\n)*?</td>");
                string d3Version = ReplaceHTML(mDataCollectionInfo[1].Value);
                string d4Release = ReplaceHTML(mDataCollectionInfo[2].Value);
                string d5Country = ReplaceHTML(mDataCollectionInfo[3].Value);
            }

            //Screenshot Tab
            string s2 = wc.DownloadString(sUri + "/images");

            //Video Tab
            string s3 = wc.DownloadString(sUri + "/videos");
        }

        private static string ReplaceHTML(string sText)
        {
            return Regex.Replace(sText, "<(.|\n)*?>", "");
        }
    }


}
